package com.atguign.gulimall.ware.dao;

import com.atguign.gulimall.ware.entity.WareSkuEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 商品库存
 *
 * @author liuxvru
 * @email liuxvru@163.com
 * @date 2024-01-13 21:45:13
 */
@Mapper
public interface WareSkuDao extends BaseMapper<WareSkuEntity> {
    @Update("UPDATE gulimall_wms.wms_ware_sku SET stock = stock + #{skuNum} WHERE sku_id = #{skuId} and ware_id = #{wareId}")
    int addStock(@Param("skuId") Long skuId, @Param("wareId") Long wareId, @Param("skuNum") Integer skuNum);

    @Select("SELECT ware_id FROM gulimall_wms.wms_ware_sku WHERE sku_id = #{skuId} and stock-wms_ware_sku.stock_locked > 0")
    List<Long> listWareIdHasSkuStock(@Param("skuId") Long skuId);
    @Update("UPDATE gulimall_wms.wms_ware_sku SET stock_locked = stock_locked + #{num} WHERE sku_id = #{skuId} and ware_id = #{id} and stock-wms_ware_sku.stock_locked>=#{num}")
    Long lockSkuStock(@Param("skuId") Long skuId,@Param("id") Long id,@Param("num") Integer num);

    @Update("UPDATE gulimall_wms.wms_ware_sku SET stock_locked = stock_locked - #{num} WHERE sku_id = #{skuId} and ware_id = #{wareId}")
    Long unLockStock(@Param("skuId")Long skuId,@Param("wareId") Long wareId,@Param("num") Integer num);
}
